var mineCondition;
var conditionPattern = false;
$(document).ready(function () {
    $("#search").click(function () {
        var conditionFromInput = $("#condition").val();
        if (conditionFromInput.replace(/^\s+|\s+$/g,"") == "") {
            return;
        }
        mineCondition = conditionFromInput;
        $("#returnAll").css("display", "block");
        $("#categoryTabContent div div").addClass("btn-default").removeClass("btn-info");
        // 移除之后，记得要到数组中删除(删除所有之前选中的)
        fileCategoryArray.forEach(function(value, index, array){
            fileCategoryArray.splice(index, 1);
        });
        conditionPattern = true;

        mineTableShow($fileTableList, $currentPage, $totalPage, $totalCount, mineCondition);

        toastr.success('查询成功');
    });

    $("#returnAll").click(function () {
        $("#returnAll").css("display", "none");
        conditionPattern = false;
        mineCondition = undefined;

        mineTableShow($fileTableList, $currentPage, $totalPage, $totalCount, mineCondition);

        toastr.success('已返回全集');
    });

    // 1.获取右下面板的资源分布图
    $.ajax({
        type: "GET",
        url: '/api/user/fileRatio',
        success:
            function (res) {
                if (res.code == 0) {
                    // 激活tooltip
                    $(function () { $("[data-toggle='tooltip']").tooltip(); });

                    var $fileCategoryRatioSpan = $("#fileCategoryRatioSpan");
                    var $fileCategoryRatioDiv = $("#fileCategoryRatioDiv");
                    var colorArray = ['#457fb1', '#13b7a8', '#c4c790', '#d27496'];
                    var colorIndex = 0;

                    $fileCategoryRatioSpan.empty();
                    $fileCategoryRatioDiv.empty();
                    $fileCategoryRatioSpan.append("以下依次为：");
                    var len = res.data.length;

                    for (var i=0; i<len; i++) {

                        if (i == len - 1) {
                            $fileCategoryRatioSpan.append("和<span data-toggle=\"tooltip\" data-placement=\"top\" title=\""+res.data[i].percents+"%\">"+res.data[i].fileCategoryLevel1+"</span>");
                        } else {
                            $fileCategoryRatioSpan.append("<span data-toggle=\"tooltip\" data-placement=\"top\" title=\""+res.data[i].percents+"%\">"+res.data[i].fileCategoryLevel1+"</span>、");
                        }

                        if (colorIndex == colorArray.length) {
                            colorIndex = 0;
                        }
                        $fileCategoryRatioDiv.append("<div title=\""+res.data[i].fileCategoryLevel1+"："+res.data[i].percents+"%\" style=\"background-color: "+colorArray[colorIndex]+"; width: "+res.data[i].percents+"%;\"></div>");
                        colorIndex++;

                    }
                }
                if (res.code == -1) {
                    toastr.error(res.msg);
                }
            },
        error:
            function (res) {
                toastr.error('系统接口异常，请稍后');
            }
    });

    // 2.获取文件类别的节点树
    $.ajax({
        type: "GET",
        url: '/api/getFileCategoryTree',
        success:
            function (res) {
                if (res.code == 0) {
                    var $categoryTab =  $("#categoryTab");
                    var $categoryTabContent = $("#categoryTabContent");
                    $categoryTab.empty();
                    $categoryTabContent.empty();
                    for (var i=0; i<res.data.length; i++) {
                        if (i == 0) {
                            $categoryTab.append("<li class=\"active\"><a href=\"#"+res.data[i].parentRemark+"Tab\" data-toggle=\"tab\">"+res.data[i].parentName+"</a></li>");

                            var node = "";
                            for (var j=0; j<res.data[i].child.length; j++) {
                                node = node + "<div class=\"btn btn-default\" onclick=\"chooseCategory(this)\"><span style='display: none'>"+res.data[i].child[j].childId+"</span>"+res.data[i].child[j].childName+"</div>";
                            }

                            $categoryTabContent.append("<div class=\"tab-pane fade in active\" id=\""+res.data[i].parentRemark+"Tab\">" + node + "</div>");
                        } else {
                            $categoryTab.append("<li><a href=\"#"+res.data[i].parentRemark+"Tab\" data-toggle=\"tab\">"+res.data[i].parentName+"</a></li>");

                            var node = "";
                            for (var j=0; j<res.data[i].child.length; j++) {
                                node = node + "<div class=\"btn btn-default\" onclick=\"chooseCategory(this)\"><span style='display: none'>"+res.data[i].child[j].childId+"</span>"+res.data[i].child[j].childName+"</div>";
                            }

                            $categoryTabContent.append("<div class=\"tab-pane fade\" id=\""+res.data[i].parentRemark+"Tab\">"+node+"</div>");
                        }
                    }
                }
                if (res.code == -1) {
                    toastr.error(res.msg);
                }
            },
        error:
            function (res) {
                toastr.error('系统接口异常，请稍后');
            }
    });

    // console.log(fileCategoryArray);
    // 4.数据渲染

    // 表格
    var $fileTableList = $("#fileTableList");
    // 当前页
    var $currentPage = $("#currentPage");
    // 总页数
    var $totalPage = $("#totalPage");
    // 总记录数
    var $totalCount = $("#totalCount");

    // 0:按时间
    var $orderByTime = $("#orderByTime");
    // 1:按下载数
    var $orderByDownload= $("#orderByDownload");
    // 2:按点赞数
    var $orderByStar = $("#orderByStar");

    // 上一页
    var $prePage = $("#prePage");
    // 下一页
    var $nextPage = $("#nextPage");

    // 跳转到xx页
    var $jumpTo = $("#jumpTo");
    // document加载时渲染
    $fileTableList.empty();
    $fileTableList.append("<tbody><tr><th>资源名称</th><th>资源类别</th><th>资源大小</th><th>下载数</th><th>点赞数</th><th>删除资源</th></tr></tbody>");
    mineTableShow($fileTableList, $currentPage, $totalPage, $totalCount, mineCondition);

    // document渲染时加载
    if ($orderByTime.hasClass("sortOrder_chosen") == true) {
        orderBy = orderByArray[0];
    }
    if ($orderByDownload.hasClass("sortOrder_chosen") == true) {
        orderBy = orderByArray[1];
    }
    if ($orderByStar.hasClass("sortOrder_chosen") == true) {
        orderBy = orderByArray[2];
    }

    // 一：排序触发
    // 0：
    $orderByTime.click(function () {
        $orderByTime.addClass("sortOrder_chosen");
        $orderByDownload.removeClass("sortOrder_chosen");
        $orderByStar.removeClass("sortOrder_chosen");
        orderBy = orderByArray[0];
        // 归 1
        $currentPage.text(1);
        // 归空
        // $jumpTo.text("");
        mineTableShow($fileTableList, $currentPage, $totalPage, $totalCount, mineCondition);
    });
    // 1:
    $orderByDownload.click(function () {
        $orderByTime.removeClass("sortOrder_chosen");
        $orderByDownload.addClass("sortOrder_chosen");
        $orderByStar.removeClass("sortOrder_chosen");
        orderBy = orderByArray[1];
        // 归 1
        $currentPage.text(1);
        // 归空
        // $jumpTo.text("");
        mineTableShow($fileTableList, $currentPage, $totalPage, $totalCount, mineCondition);
    });
    // 2:
    $orderByStar.click(function () {
        $orderByTime.removeClass("sortOrder_chosen");
        $orderByDownload.removeClass("sortOrder_chosen");
        $orderByStar.addClass("sortOrder_chosen");
        orderBy = orderByArray[2];
        // 归 1
        $currentPage.text(1);
        // 归空
        // $jumpTo.text("");
        mineTableShow($fileTableList, $currentPage, $totalPage, $totalCount, mineCondition);
    });

    // 上一页被点击
    $prePage.click(function () {
        var currentPage = $currentPage.text();
        pageNum = parseInt(currentPage) - 1;

        mineTableShow($fileTableList, $currentPage, $totalPage, $totalCount, mineCondition);
    });
    // 下一页被点击
    $nextPage.click(function () {
        var currentPage = $currentPage.text();
        pageNum = parseInt(currentPage) + 1;

        mineTableShow($fileTableList, $currentPage, $totalPage, $totalCount, mineCondition);
    });

    // 失去焦点：ajax
    $jumpTo.focusout(function() {
        var num = $jumpTo.val();
        console.log(num);


        num = parseInt(num);
        console.log(num);
        if (parseInt(num).toString() == "NaN") {
            toastr.error('请输入数字')
        } else {
            pageNum = parseInt(num);
            mineTableShow($fileTableList, $currentPage, $totalPage, $totalCount, mineCondition);
            toastr.success('跳转成功');
        }
    });
});

